package cc.mrbird.febs.accounting.service.impl;

import cc.mrbird.febs.accounting.dto.detailCost.DetailCostDTO;
import cc.mrbird.febs.accounting.dto.detailCost.DetailCostParam;
import cc.mrbird.febs.accounting.entity.*;
import cc.mrbird.febs.accounting.mapper.MainCostMapper;
import cc.mrbird.febs.accounting.service.IDetailCostService;
import cc.mrbird.febs.accounting.service.IMainCostService;
import cc.mrbird.febs.utils.ArithUtil;
import cc.mrbird.febs.utils.CodeConstant;
import cc.mrbird.febs.common.utils.FebsUtil;
import cc.mrbird.febs.utils.PermissionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

/**
 *  Service实现
 *
 * @author hbh
 * @date 2021-09-28 23:07:50
 */
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
public class MainCostServiceImpl extends ServiceImpl<MainCostMapper, MainCost> implements IMainCostService {

    @Resource
    private IDetailCostService iDetailCostService;

    @Override
    public List<StandardMain> queryMainCost(String yearMonth) {
        List<DetailCostDTO> mainCosts = this.baseMapper.queryMainCostByYearMonth(yearMonth);
        // 计算增值税进项调整
        LambdaQueryWrapper<DetailCost> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DetailCost::getYearMonth, yearMonth);
        queryWrapper.eq(DetailCost::getIsDeleted, 0);
        List<DetailCost> inputTaxAdjustments = iDetailCostService.getBaseMapper().selectList(queryWrapper);
        double cost = 0;
        for (DetailCost e : inputTaxAdjustments) {
            if (e.getInputTaxAdjustment() != null) {
                cost = ArithUtil.add(cost, e.getInputTaxAdjustment().doubleValue());
            }
        }

        List<StandardMain> standardMains = new ArrayList<>();
        Set<String> codes = new HashSet<>();
        mainCosts.forEach(e -> {
            if (!codes.contains(e.getMainSubjectCode())) {
                codes.add(e.getMainSubjectCode());
                StandardMain standardMain = new StandardMain();
                standardMain.setCode(e.getMainSubjectCode());
                standardMain.setName(e.getMainSubjectName());
                standardMains.add(standardMain);
            }
        });
        for (DetailCostDTO e : mainCosts) {
            for (StandardMain m : standardMains) {
                if (e.getMainSubjectCode().equals(m.getCode())) {
                    if (e.getDetailSubjectCode().equals(CodeConstant.inputTaxAdjustment)) {
                        e.setCost(cost);
                    }
                    List<DetailCostDTO> detailCostList = m.getDetailCostDTOS() == null ? new ArrayList<>() : m.getDetailCostDTOS();
                    detailCostList.add(e);
                    m.setDetailCostDTOS(detailCostList);
                }
            }
        }
        return standardMains;
    }

    @Override
    public void saveDetailCost(DetailCostParam detailCostParam) {
        LambdaQueryWrapper<MainCost> deleteWrapper = new LambdaQueryWrapper<>();
        deleteWrapper.eq(MainCost::getYearMonth, detailCostParam.getYearMonth());
        this.remove(deleteWrapper);
        if (detailCostParam.getDetailCostList().size() == 0) {
            return;
        }
        List<MainCost> mainCosts = new ArrayList<>();
        detailCostParam.getDetailCostList().forEach(e -> {
            if (!e.getStandardSubCode().equals(CodeConstant.inputTaxAdjustment)) {
                MainCost mainCost = new MainCost();
                mainCost.setCost(e.getCost());
                mainCost.setEstimate(e.getEstimate());
                mainCost.setDetailSubjectCode(e.getStandardSubCode());
                mainCost.setRemark(e.getRemark());
                mainCost.setYearMonth(detailCostParam.getYearMonth());
                mainCost.setType(1);
                mainCost.setIsDeleted(0);
                mainCost.setCreateBy(FebsUtil.getCurrentUser().getUsername());
                mainCost.setCreateTime(new Date());
                mainCosts.add(mainCost);
            }
        });
        this.saveBatch(mainCosts);
    }
}
